package plugins.openform;

import kd.bos.context.RequestContext;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.form.FormShowParameter;
import kd.bos.form.events.BeforeCreateListColumnsArgs;
import kd.bos.form.events.SetFilterEvent;
import kd.bos.list.plugin.AbstractListPlugin;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.sdk.plugin.Plugin;

/**
 * 标准单据列表插件
 */
public class OpenOrderHistory extends AbstractListPlugin implements Plugin {

    public String Model = "";
    public String Identity = "";

    @Override
    public void beforeCreateListColumns(BeforeCreateListColumnsArgs args) {
        super.beforeCreateListColumns(args);

        // 获取入口参数
        FormShowParameter param = this.getView().getFormShowParameter();
        String model = param.getCustomParam("Model"); // 获取模式参数
        String identity = param.getCustomParam("Identity"); // 获取身份参数

        // 如果参数不存在或为null，设置默认值
        Model = model != null ? model : "";
        Identity = identity != null ? identity : "";

        // 动态隐藏指定字段
        if ("Dine-in".equals(model)) {
            args.getListColumns().stream()
                    .filter(col -> "dcb4_delivery".equals(col.getListFieldKey()))
                    .forEach(col -> col.setVisible(0));

            args.getListColumns().stream()
                    .filter(col -> "dcb4_address".equals(col.getListFieldKey()))
                    .forEach(col -> col.setVisible(0));
        }
    }

    @Override
    public void setFilter(SetFilterEvent e) {
        super.setFilter(e);

        // this.getView().showTipNotification("当前Model: " + Model);

        // 根据Model设置过滤条件
        if ("Takeaway".equals(Model)) {

            if ("user".equals(Identity)) {
                // 如果身份是用户，就只展示当前用户的外卖订单历史
                String currentUser = RequestContext.get().getUserName();

                // this.getView().showTipNotification("当前用户: " + currentUser);

                // QFilter userFilter = new QFilter("dcb4_buyer", QCP.equals, currentUser);
                QFilter filter = new QFilter("dcb4_address", QCP.not_equals, "").and(new QFilter("dcb4_buyer", QCP.equals, currentUser));
                e.addCustomQFilter(filter);
            } else if("delivery".equals(Identity)) {
                // 如果身份是配送员，就只展示当前配送员的外卖订单历史
                String currentUser = RequestContext.get().getUserName();

                // this.getView().showTipNotification("当前用户T: " + currentUser);
                // this.getView().showTipNotification("当前身份T: " + Identity);

                QFilter filter = new QFilter("dcb4_address", QCP.not_equals, "").and(new QFilter("dcb4_delivery", QCP.equals, currentUser));
                e.addCustomQFilter(filter);
            } else if ("merchant".equals(Identity)) {
                // 如果身份是商户，就展示当前商户的所有的外卖订单历史
                // this.getView().showTipNotification("当前身份: " + Identity);

                String currentUser = RequestContext.get().getUserName();

                // this.getView().showTipNotification("当前用户: " + currentUser);

                QFilter userFilter = new QFilter("dcb4_manager_name", QCP.equals, currentUser);
                DynamicObject record = BusinessDataServiceHelper.loadSingle(
                        "dcb4_merchantdata",
                        "id",
                        new QFilter[]{userFilter}
                );

                // this.getView().showTipNotification("当前商户名: " + record.get("name"));

                QFilter filter = new QFilter("dcb4_address", QCP.not_equals, "").and(new QFilter("dcb4_merchant", QCP.equals, record.getPkValue()));
                e.addCustomQFilter(filter);
            } else {
                // 如果身份是管理员，就展示所有的外卖订单历史
                QFilter filter = new QFilter("dcb4_address", QCP.not_equals, "");
                e.addCustomQFilter(filter);
            }

        } else if ("Dine-in".equals(Model)) {
            if ("user".equals(Identity)) {
                // 如果身份是用户，就只展示当前用户的堂食订单历史
                String currentUser = RequestContext.get().getUserName();

                // this.getView().showTipNotification("当前用户: " + currentUser);

                QFilter filter = new QFilter("dcb4_address", QCP.equals, "").and(new QFilter("dcb4_buyer", QCP.equals, currentUser));
                // QFilter filter = new QFilter("dcb4_address", QCP.equals, "");
                e.addCustomQFilter(filter);
            } else if("merchant".equals(Identity)) {
                // 如果身份是商户，就展示当前商户的所有的堂食订单历史
                // this.getView().showTipNotification("当前身份: " + Identity);

                String currentUser = RequestContext.get().getUserName();

                // this.getView().showTipNotification("当前用户: " + currentUser);

                QFilter userFilter = new QFilter("dcb4_manager_name", QCP.equals, currentUser);
                DynamicObject record = BusinessDataServiceHelper.loadSingle(
                        "dcb4_merchantdata",
                        "id",
                        new QFilter[]{userFilter}
                );

                // this.getView().showTipNotification("当前商户名: " + record.get("name"));

                QFilter filter = new QFilter("dcb4_address", QCP.equals, "").and(new QFilter("dcb4_merchant", QCP.equals, record.getPkValue()));
                e.addCustomQFilter(filter);
            } else {
                // 如果身份是商户（管理员），就展示所有的堂食订单历史
                QFilter filter = new QFilter("dcb4_address", QCP.equals, "");
                e.addCustomQFilter(filter);
            }
        } else {
            super.setFilter(e);
        }
    }



}